package com.embhealth.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.embhealth.pojo.MemberFile;
import com.embhealth.service.MemberService;
import com.embhealth.utils.MD5Utils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.embhealth.dao.MemberDao;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;
    //member分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//ThreadLocal
        PageHelper.startPage(currentPage,pageSize);//分页插件，会在执行sql之前将分页关键字追加到SQL后面
        Page<Member> page = memberDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }
    //添加member会员管理
    public void add_member(Member member) throws Exception {
        member.setRegTime(new Date());//设置当前时间
        //计算生日设计时间
        Date birthDay = member.getBirthday();
        int age = 0;
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) { //出生日期晚于当前时间，无法计算
            throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);  //当前年份
        int monthNow = cal.get(Calendar.MONTH);  //当前月份
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
        cal.setTime(birthDay);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        age = yearNow - yearBirth;   //计算整岁数
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;//当前日期在生日之前，年龄减一
            } else {
                age--;//当前月份在生日之前，年龄减一
            }
        }
        member.setAge(String.valueOf(age));
        memberDao.add_member(member);


    }
    //编辑member会员管理
    public void edit_member(Member member) {
        memberDao.edit_member(member);
    }
    //根据手机号查询会员信息
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

//    //新增会员
//    public void add(Member member) {
//        //如果用户设置了密码，需要对密码进行md5加密
//        String password = member.getPassword();
//        if(password != null){
//            password = MD5Utils.md5(password);
//            member.setPassword(password);
//        }
//        memberDao.add(member);
//    }



    //根据月份查询会员数量
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCounts = new ArrayList<>();
        if(months != null && months.size() > 0){
            for (String month : months) {//2019.01
                String endTime = month + ".31";
                Integer memberCount = memberDao.findMemberCountBeforeDate(endTime);
                memberCounts.add(memberCount);
            }
        }
        return memberCounts;
    }
    //根据id删除检查项
    public void deleteById(Integer id) {
            //可以删除
        memberDao.deleteById(id);

    }
    /**
     * 批量删除
     */
    public void del_member(Map map) {
        List<Integer> ids =(List<Integer>) map.get("ids");
        memberDao.del_member(ids);
    }

    @Override
    public void add(Member member) {
        String password=member.getPassword();
        if (password!=null){
            //使用MD5加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    @Override
    public void edit(Member member){
        memberDao.edit(member);
    }

    @Override
    public Map<String, Object> findAgeGroup() {
        //封装年龄段名称和数量
        Map<String, Object> map = new HashMap<>();
        //接收年龄段名称
        List<String> ageGroups = new ArrayList<>();
        //接收年龄段对应的数量
        List<Integer> numbers = new ArrayList<>();
        //调用dao层获取年龄段和数量
        List<Map<String, Object>> list = memberDao.ageGroup();
        for (Map<String, Object> sMap : list) {
            String ageGroup1 = (String) sMap.get("ageGroup");
            ageGroups.add(ageGroup1);
            Long number = (Long) sMap.get("number");
            numbers.add(Math.toIntExact(number));
        }
        map.put("ageGroups", ageGroups);
        map.put("numbers", numbers);
        return map;
    }

    @Override
    public MemberFile getmemberfile(String tel){
        //获取会员信息
        Member member = memberDao.findByTelephone(tel);
        try {
            String fileNumber = member.getFileNumber();
            return memberDao.getmemberfile(fileNumber);
        }catch (Exception e) {
            //查询档案信息
            return null;
        }
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }
}
